/**
 * 
 */
package org.okhati.www.model;

import jxl.Cell;

/**
 * @author sujan
 *
 */
public class InstitutionFactory implements Factory<Institution> {

	@Override
	public Institution getFactoryObject(Cell[] cells) {
		try {
			Float.parseFloat(cells[0].getContents());
		} catch(NumberFormatException nfe) {
			return null;
		}
		Institution institution = new Institution();
		institution.setSn(cells[0].getContents());
		institution.setType(getType(cells[1].getContents(), cells[0].getContents(), cells[3].getContents()));
		String name = cells[2].getContents();
		if(name.indexOf(',') > 0) {
			institution.setName(name.substring(0, name.indexOf(',')));
		} else {
			institution.setName(name);
		}
		institution.setDistrict(cells[3].getContents());
		institution.setRegion(cells[4].getContents());
		institution.setCity(cells[5].getContents());
		institution.setWard(cells[6].getContents());
		institution.setBedCount(getBedCount(cells[7].getContents()));
		return institution;
	}

	private int getBedCount(String contents) {
		return Integer.parseInt(contents);
	}

	private String getType(String type, String sn, String district) {
		Integer typeInt = (int) Float.parseFloat(sn);
		String typeKey = typeInt + district;
		InstitutionRegistry registry = InstitutionRegistry.getInstance();
		if(!registry.knowsType(typeKey)) {
			registry.addToMap(typeKey, type);
		}
		return registry.getType(typeKey);
	}


}
